<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :span="12">
              <a-button icon="plus" style="margin-right: 8px" type="primary" @click="handleAdd">新建</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column label="标签名称" prop="labelName" align="center" />
        <el-table-column label="排序号" prop="sortOrder" align="center" />
        <el-table-column label="创建时间" prop="createTime" align="center" />
        <el-table-column label="更新时间" prop="updateTime" align="center" />
        <el-table-column width="150" fixed="right" align="center" label="操作">
          <template slot-scope="scope">
            <el-link :underline="false" type="primary" 
            @click="handleEdit(scope.row)">编辑</el-link>
            <el-link :underline="false" type="danger" style="margin-left:10px;"
            @click="handleDelete(scope.row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>

      <div style="display: flex;justify-content: flex-end;margin: 20px 0 0;">
        <el-pagination :current-page="queryParam.page" :page-size="queryParam.limit" :page-sizes="[10, 20, 50, 100]"
          :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
          @current-change="handleCurrentChange">
        </el-pagination>
      </div>
    </el-card>

    <a-modal :title="modal.title" :visible="modal.visible" maskClosable width="600px" @cancel="modal.visible = false"
      @ok="handleOk">
      <a-form :form="form" :label-col="{ span: 3 }" :wrapper-col="{ span: 16 }">
        <a-form-item label="id" style="display: none;">
          <a-input v-decorator="['id']" />
          <a-input v-decorator="['productId']" />
          <a-input v-decorator="['labelType']" />
        </a-form-item>
        
        <a-form-item label="标签名称">
          <a-input v-decorator="['labelName', { rules: [{ required: true, message: '请输入【标签名称】' }] }]" />
        </a-form-item>

        <a-form-item label="排序号">
          <a-input-number :min="1" :precision="0" 
            v-decorator="['sortOrder', { rules: [{ required: true, message: '请输入【排序号】' }] }]" 
            :style="{ width: '100%', textAlign: 'center', display: 'inline-block' }" />
        </a-form-item>

      </a-form>
    </a-modal>

  </div>
</template>

<script>
  import {
    addProductLabel,
    listByPageProductLabel,
    updateProductLabel,
    deleteProductLabel
  } from '@/api/aibao/product/label'

  import pick from 'lodash.pick'

  const baseApi = process.env.VUE_APP_API_BASE_URL
  // 表单字段
  const fields = ['id', 'productId', 'labelName', 'labelType', 'sortOrder']

  export default {
    name: 'ProductListLabel',
    props: {
      productId: {
        type: String,
        default: ''
      },
    },
    data() {
      return {
        queryParam: {
          page: 1,
          limit: 10,
          productId: ''
        },
        labelList: [{
            id: 1,
            value: '1',
            labelType: "系统标签"
          },
          {
            id: 2,
            value: '2',
            labelType: "用户标签"
          }
        ],
        total: 0,
        modal: {
          visible: false,
          title: '',
          form: {}
        },
        tableData: [],
        form: this.$form.createForm(this)
      }
    },
    created() {
      this.queryParam.productId = this.productId
      this.getTable()
      fields.forEach(v => this.form.getFieldDecorator(v))
      this.$watch('modal', () => {
        if (this.modal) {
          this.form.setFieldsValue(pick(this.modal.form, fields))
        }
      })
    },
    methods: {
      query() {
        this.queryParam.page = 1
        this.getTable()
      },
      handleSizeChange(val) {
        this.queryParam.limit = val
        this.getTable()
      },
      handleCurrentChange(val) {
        this.queryParam.page = val
        this.getTable()
      },

      getTable() {
        this.queryParam.productId = this.productId
        listByPageProductLabel(this.queryParam).then(res => {
          this.tableData = res.data.records
          this.total = parseInt(res.data.total)
        }).catch(() => {})
      },
      handleOk() {
        this.form.validateFields((errors, values) => {
          if (!errors) {
            if (values.id) {
              // 修改
              updateProductLabel(values).then(res => {
                this.modal.form = {}
                this.modal.visible = false
                this.queryParam.page = 1
                this.getTable()
                this.$message.success('修改成功')
              }).catch(() => {})
            } else {
              // 新增
              addProductLabel(values).then(res => {
                this.modal.form = {}
                this.modal.visible = false
                this.queryParam.page = 1
                this.getTable()
                this.$message.success('新增成功')
              }).catch(() => {})
            }
          }
        })
      },
      handleAdd() {
        this.modal.form = {
          productId: this.productId,
          labelName: '',
          labelType: 1,
          sortOrder: 1,
          id: ''
        }
        this.form.setFieldsValue(pick(this.modal.form, fields))
        this.modal.visible = true
        this.modal.title = '新增'
      },

      handleEdit(record) {
        const data = JSON.parse(JSON.stringify(record))
        this.modal.form = {
          productId: data.productId,
          labelName: data.labelName,
          labelType: data.labelType,
          sortOrder: data.sortOrder,
          id: data.id
        }
        this.form.setFieldsValue(pick(this.modal.form, fields))
        this.modal.visible = true
        this.modal.title = '编辑'
      },
      handleDelete(record) {
        // 删除，2025年1月18日17:17:04
        deleteProductLabel([record.id]).then(res => {
          this.queryParam.page = 1
          this.getTable()
          this.$message.success('删除成功')
        }).catch(() => {})
      }
    }
  }
</script>

<style lang="less" scoped>
  .view-video {
    position: relative;

    .close {
      position: absolute;
      right: 15px;
      top: 0;
      cursor: pointer;
    }
  }

  /deep/.ant-modal-body {
    max-height: calc(80vh - 150px);
    overflow-y: auto;

    &::-webkit-scrollbar {
      width: 6px;
      /*高宽分别对应横竖滚动条的尺寸*/
      height: 1px;
    }

    &::-webkit-scrollbar-thumb {
      // -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      background: #e3e3e6;
      border-radius: 6px;
    }

    &::-webkit-scrollbar-track {
      background: transparent;
      // -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      border-radius: 5px;
    }
  }
</style>